package com.ruoyi.tws.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.WebsocketConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.common.core.web.page.Query;
import com.ruoyi.tws.domain.TwsQuestion;
import com.ruoyi.tws.service.ITwsNoticeService;
import com.ruoyi.tws.service.ITwsQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 手机端 - 咨询投诉controller
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping(Constants.APP_URL + "/")
public class AppQuestionController {

    private final ITwsQuestionService twsQuestionService;
    private final ITwsNoticeService twsNoticeService;

    /** 新增 */
    @PostMapping("question-add")
    @Transactional
    public R add(@RequestBody TwsQuestion twsQuestion) {
//        twsQuestion.setDriverId(SecurityUtils.getUserId());
        if (twsQuestion.getType() == 1){
            twsNoticeService.newTwsNotice("admin", WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT.getName());
        }else if (twsQuestion.getType() == 2){
            twsNoticeService.newTwsNotice("admin", WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN.getName());
        }
        return R.ok(twsQuestionService.save(twsQuestion));
    }

    /** 列表 */
    @GetMapping("question-list")
    public R<IPage<TwsQuestion>> questionList(Query query,@RequestParam Long status,Long type){
        LambdaQueryWrapper<TwsQuestion> lqw = new LambdaQueryWrapper<>();
        if (status == 2){
            lqw.eq(TwsQuestion::getDriverId, SecurityUtils.getUserId());
        }
        if (status == 1){
            lqw.eq(TwsQuestion::getCompanyId,SecurityUtils.getUserId());
        }
        lqw.eq(TwsQuestion::getStatus,status);
        lqw.eq(TwsQuestion::getType,type);
        lqw.orderByDesc(TwsQuestion::getCreateTime);
        IPage<TwsQuestion> list = twsQuestionService.page(Condition.getPage(query),lqw);
        return R.ok(list);
    }

    /**
     * 问题详细
     */
    @PostMapping("question-detail")
    public R questionDetail(@RequestBody Long id){
        return R.ok(twsQuestionService.getById(id));
    }

}
